package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.common.constant.ConstantsEnum;
import com.twetec.cms.dao.mapper.system.SystemApiWhiteListMapper;
import com.twetec.cms.model.operter.CmsOperter;
import com.twetec.cms.model.po.system.SystemApiWhiteListPO;
import com.twetec.cms.model.vo.system.WhitListPageListVO;
import com.twetec.cms.model.vo.system.WhiteListGetOneVO;
import com.twetec.cms.model.vo.system.WhiteListVO;
import com.twetec.cms.service.system.SystemApiWhiteListService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-03-16
 */
@Service
public class SystemApiWhiteListServiceImpl extends ServiceImpl<SystemApiWhiteListMapper, SystemApiWhiteListPO> implements SystemApiWhiteListService {


    @Override
    public boolean create(String name, String keyId, String keySecret, String ipAddrList, String remark, Integer status, Long userId) {

        SystemApiWhiteListPO systemApiWhiteListPO = new SystemApiWhiteListPO();
        systemApiWhiteListPO.setName(name);
        systemApiWhiteListPO.setKeyId(keyId);
        systemApiWhiteListPO.setKeySecret(keySecret);
        systemApiWhiteListPO.setIpAddrList(ipAddrList);
        if (StringUtils.isBlank(remark)) {
            systemApiWhiteListPO.setRemark("");
        }else{
            systemApiWhiteListPO.setRemark(remark);
        }
        systemApiWhiteListPO.setCreatorId(userId);
        systemApiWhiteListPO.setModifierId(userId);
        systemApiWhiteListPO.setStatus(status);

        return this.save(systemApiWhiteListPO);
    }

    @Override
    public boolean updateById(Long id, String name, String keyId, String keySecret, String ipAddrList, String remark, Long userId) {
        SystemApiWhiteListPO systemApiWhiteListPO = new SystemApiWhiteListPO();
        systemApiWhiteListPO.setId(id);
        systemApiWhiteListPO.setName(name);
        systemApiWhiteListPO.setKeyId(keyId);
        systemApiWhiteListPO.setKeySecret(keySecret);
        systemApiWhiteListPO.setIpAddrList(ipAddrList);
        if (StringUtils.isBlank(remark)) {
            systemApiWhiteListPO.setRemark("");
        }else{
            systemApiWhiteListPO.setRemark(remark);
        }
        systemApiWhiteListPO.setModifierId(userId);
        return super.updateById(systemApiWhiteListPO);
    }

    @Override
    public boolean removeById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("IS_DELETED", 1);
        updateWrapper.eq("ID", id);
        boolean update = super.update(updateWrapper);
        return update;
    }

    @Override
    public boolean updateStatusById(Long id, Integer status, Long userId) {
        if (null == id || id <= 0){
            return false;
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("ID", id);
        updateWrapper.set("STATUS", status);
        updateWrapper.set("MODIFIER_ID", userId);
        Date date =  new Date(System.currentTimeMillis());
        updateWrapper.set("GMT_MODIFIED", date);
        boolean update = super.update(updateWrapper);
        return update;
    }

    @Override
    public WhiteListGetOneVO getById(Long id) {

        if (null == id || id <= 0){
            return null;
        }
        SystemApiWhiteListPO systemApiWhiteListPO = super.getById(id);
        if (null == systemApiWhiteListPO){
            return null;
        }
        WhiteListGetOneVO whiteListGetOneVO = new WhiteListGetOneVO();
        BeanUtils.copyProperties(systemApiWhiteListPO, whiteListGetOneVO);
        return whiteListGetOneVO;
    }

}
